package ucuserslogic

import (
	"context"
	"go.uber.org/zap"
	"navi-mumbai/app/rpc/model/pg/usercenter"
	"navi-mumbai/common/key/dbkey"
	"navi-mumbai/common/utils"
	"navi-mumbai/common/xerr"

	"navi-mumbai/app/rpc/usercenter/internal/svc"
	"navi-mumbai/app/rpc/usercenter/pb"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetUserPayAccountListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetUserPayAccountListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserPayAccountListLogic {
	return &GetUserPayAccountListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 获取用户支付账户列表
func (l *GetUserPayAccountListLogic) GetUserPayAccountList(in *pb.GetUserPayAccountListReq) (*pb.GetUserPayAccountListResp, error) {
	defer utils.RecoverPanic()

	var err error
	var list []*usercenter.UserPayAccountAddress
	list, err = l.svcCtx.UserPayAccountAddressModel.Find(l.ctx, in.Uid, in.PageNo, in.PageSize)
	if err != nil {
		l.Logger.Errorf("err == ", zap.Error(err))
		return nil, xerr.NewGrpcErrCodeMsg(xerr.DbError, err.Error())
	}
	var rsp pb.GetUserPayAccountListResp
	rsp.List = make([]*pb.PayAccount, 0)
	if len(list) <= 0 {
		return &rsp, nil
	}

	for i := 0; i < len(list); i++ {
		rsp.List = append(rsp.List, &pb.PayAccount{
			Id:             list[i].Id,
			Uid:            list[i].Uid,
			BankCode:       list[i].BankCode,
			BankName:       list[i].BankName,
			BankCardNumber: list[i].BankCardNumber,
			AccountType:    list[i].AccountType,
			AccountName:    list[i].AccountName,
			CreateTime:     list[i].CreateTime.Format(dbkey.DateTimeCNFormat),
			Remark:         list[i].Remark,
		})
	}
	return &rsp, nil
}
